<template>
	<view class="page">
		<view class="common">
			<view class="login_text">
				登&nbsp;录
			</view>
			<view class="forms">
				<uni-forms ref="baseForm" :modelValue="baseFormData" label-position="top">
					<uni-forms-item label="账号:">
						<!-- 	  <uni-icons type="right" size="22"></uni-icons> -->
						<uni-easyinput type="text" v-model="baseFormData.username" placeholder="请输入账号" />
					</uni-forms-item>
					<uni-forms-item label="密码:">
						<uni-easyinput type="password" v-model="baseFormData.password" placeholder="请输入密码" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" style="background-color: #4381e4;"
					@click="submit(baseFormData)">登&emsp;录</button>
			</view>
			<view class="forgetPsd" @click="showToests">忘记密码?</view>
		</view>
	</view>
</template>

<script>
	import {
		Login,
		codeCheck
	} from "@/api/api.js";
	import tools from "@/utils/tools.js"
	import {
		URL
	} from "@/api/BaseURL.js"
	import {
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				baseFormData: {
					username: '',
					password: ''
				},
			};
		},
		methods: {
			...mapMutations(['login']),
			submit(val) {
				let dataAes = tools.encryption({
					data: this.baseFormData,
					key: 'tongjiwisdom_key',
					param: ['password']
				});
				let dept = {
					randomStr: "blockPuzzle",
					grant_type: "password",
				}
				if (!!val.password && !!val.username) {
					try {
						uni.request({
							url: `${URL}/oauth/token?randomStr=${dept.randomStr}&grant_type=${dept.grant_type}`,
							method: "POST",
							header: {
								'Authorization': "Basic cGlnOnBpZw==",
								'Content-Type': "application/x-www-form-urlencoded"
							},
							data: {
								...dataAes,
							},
							success: (res) => {
								console.log(res);
								if (res.statusCode !== 200) {
									uni.showToast({
										icon: 'error',
										title: '请核对账号密码'
									})
									return
								}
								if (res.statusCode === 200) {
									this.login(res.data)
									uni.getStorageSync("token") ? uni.clearStorageSync('token') : uni
										.setStorageSync('token', res.data.access_token)
									uni.showToast({
										icon: 'success',
										title: '登录成功'
									})
									setTimeout((e) => {
										uni.reLaunch({
											url: '../index/index'
										})
									}, 1000)
								}
							}
						})
					} catch (e) {
						uni.showToast({
							icon: 'error',
							title: '登录失败'
						})
					}
				} else {
					uni.showToast({
						icon: 'error',
						title: '请输入账号/密码'
					})
				}
			},
			showToests() {
				uni.showModal({
					title: '请联系管理员修改密码',
					confirmText: "好的",
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.page {
		.common {
			display: flex;
			flex-direction: column;

			.login_text {
				height: 20vh;
				color: #4381e4;
				line-height: 20vh;
				font-size: 65rpx;
			}

			.forms {
				height: 70vh;
			}

			.forgetPsd {
				height: 10vh;
				color: #666666;
				line-height: 10vh;
				text-align: center;
			}
		}
	}
</style>
